package md.lanjing.com.module_deal_center.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import md.lanjing.com.module_deal_center.R;
import md.lanjing.com.module_deal_center.R2;
import md.lanjing.com.module_deal_center.bean.ConsignOrderStateResponce;
import com.lxh.baselibray.constant.Constant;
import md.lanjing.com.module_deal_center.contact.ConsignOrderStateContact;
import md.lanjing.com.module_deal_center.ui.adapter.ConsignOrderStateAdapter;
import retrofit2.Response;

/**
 * 寄售订单-已锁定、审核中、已完成
 */
public class ConsignOrderStateFragment extends MvpFragment<ConsignOrderStateContact.ConsignOrderStatePresent> implements ConsignOrderStateContact.IConsignOrderStateView {
    private static final String TYPE  ="type";
    @BindView(R2.id.rv)
    RecyclerView rv;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;

    List<ConsignOrderStateResponce.DataBean> mList;
    ConsignOrderStateAdapter mAdapter;
    private int pageNo=1;
    private int type=1;
    public static ConsignOrderStateFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(TYPE, type);
        ConsignOrderStateFragment fragment = new ConsignOrderStateFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void initData(Bundle savedInstanceState) {
        type = getArguments().getInt(TYPE);

        mList=new ArrayList<>();
        mAdapter=new ConsignOrderStateAdapter(R.layout.recy_item_consign_state,mList);
        mAdapter.setType(type);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        rv.setLayoutManager(manager);
        rv.setAdapter(mAdapter);

        mPresent.queryConsignOrderStateList(context, type,pageNo);
        refresh.setOnRefreshListener(refreshLayout -> {
            pageNo=1;
            mPresent.queryConsignOrderStateList(context, this.type,pageNo);
        });
        refresh.setOnLoadMoreListener(refreshLayout -> {
            pageNo++;
            mPresent.queryConsignOrderStateList(context, this.type,pageNo);
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_consign_order_state;
    }


    @Override
    public void getConsignOrderStateListData(Response<ConsignOrderStateResponce> response) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            List<ConsignOrderStateResponce.DataBean> data = response.body().getData();
            if (pageNo==1){
                mList.clear();
            }
            mList.addAll(data);
            mAdapter.notifyDataSetChanged();
            if (!ObjectUtils.isEmpty(data)&&data.size()<10){
                refresh.setEnableLoadMore(false);
            }else {
                refresh.setEnableLoadMore(true);
            }
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    protected ConsignOrderStateContact.ConsignOrderStatePresent createPresent() {
        return new ConsignOrderStateContact.ConsignOrderStatePresent();
    }
}
